package com.cjx.yuanshenstartforumbackend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectPage;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjx.yuanshenstartforumbackend.common.BaseResponse;
import com.cjx.yuanshenstartforumbackend.common.ErrorCode;
import com.cjx.yuanshenstartforumbackend.common.ResultUtils;
import com.cjx.yuanshenstartforumbackend.domain.Article;
import com.cjx.yuanshenstartforumbackend.domain.dto.ArticleQueryDto;
import com.cjx.yuanshenstartforumbackend.domain.dto.InsertArticleDto;
import com.cjx.yuanshenstartforumbackend.exception.MyException;
import com.cjx.yuanshenstartforumbackend.service.ArticleService;
import com.cjx.yuanshenstartforumbackend.mapper.ArticleMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
* @author cjx
* @description 针对表【article(文章表)】的数据库操作Service实现
* @createDate 2023-12-23 19:23:43
*/
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article>
    implements ArticleService{

    @Resource
    private ArticleMapper articleMapper;

    @Override
    public BaseResponse<Map<String, Object>> queryPage(ArticleQueryDto articleQueryDto) {
        if (articleQueryDto == null){
            throw new MyException(ErrorCode.NULL_ERROR);
        }
        Page<Article> articlePage = new Page<>(articleQueryDto.getPageNum(), articleQueryDto.getPageSize());
        String keyWords = articleQueryDto.getKeyWords();
        Long type = articleQueryDto.getType();
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        if (!StrUtil.isBlank(keyWords)){
            wrapper.like("title",keyWords);
        }
        if (type != null && type != 0){
            wrapper.eq("category",type);
        }
        IPage<Article> pageModel = baseMapper.selectPage(articlePage, wrapper);

        //将数据进行封装
        Map<String,Object> pageInfo = new HashMap<>();
        pageInfo.put("pageDate",pageModel.getRecords());
        pageInfo.put("pageNum",pageModel.getCurrent());
        pageInfo.put("pageSize",pageModel.getSize());
        pageInfo.put("totalPage",pageModel.getPages());
        pageInfo.put("totalSize",pageModel.getTotal());
        return ResultUtils.success(pageInfo);

    }

    @Override
    public BaseResponse<?> addReading(Long aid) {
        Article article = getById(aid);
        article.setReadingQuantity(article.getReadingQuantity() + 1);
        boolean flag = updateById(article);
        if (!flag){
            throw new RuntimeException();
        }
        return ResultUtils.success();
    }

    @Override
    public BaseResponse<Article> getArticle(Long aid) {
        Article article = getById(aid);
        return ResultUtils.success(article);
    }

    @Override
    public BaseResponse<?> insertArticle(InsertArticleDto insertArticleDto) {
       //将insertArticleDto 转换成 Article
        Article article = BeanUtil.copyProperties(insertArticleDto, Article.class);
        save(article);
        return ResultUtils.success();
    }
}




